package example.SSHConnection.Ganymedexample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;


public class SSHConnectionManager {
	Connection conn = null;
	Session sess = null;
	
	public String createConnection(String host,String userName, String password) throws IOException{
		
			conn = new Connection(host);
			
			/* Now connect */

			conn.connect();

			/* Authenticate.
			 * If you get an IOException saying something like
			 * "Authentication method password not supported by the server at this stage."
			 * then please check the FAQ.
			 */

			boolean isAuthenticated = conn.authenticateWithPassword(userName, password);

			if (isAuthenticated == false)
				throw new IOException("Authentication failed.");

			/* Create a session */

			//sess = conn.openSession();
			
			return null;
		
	}
	
	public String executeComm(String command) throws IOException{
		String responce = "";
			sess = conn.openSession();			
			sess.execCommand(command);
	
			/* 
			 * This basic example does not handle stderr, which is sometimes dangerous
			 * (please read the FAQ).
			 */
	
			InputStream stdout = new StreamGobbler(sess.getStdout());
	
			BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
	
			while (true)
			{
				String line = br.readLine();
				if (line == null)
					break;
				//System.out.println(line);
				responce = responce + line;
			}
			sess.close();
		
		return responce;
	}
}
